package com.example.yrdev.ordering_system.homeOptions;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;

import com.example.yrdev.ordering_system.Adapter.GridViewAdapter;
import com.example.yrdev.ordering_system.Common.CommonActivity;
import com.example.yrdev.ordering_system.Database.DingHanDB;
import com.example.yrdev.ordering_system.Main_Menu.MenuCancelPurchase;
import com.example.yrdev.ordering_system.Main_Menu.MenuReportActivity;
import com.example.yrdev.ordering_system.Main_Menu.MenuReturnGoodActivity;
import com.example.yrdev.ordering_system.Main_Menu.MenuSettingActivity;
import com.example.yrdev.ordering_system.Model.Operator;
import com.example.yrdev.ordering_system.R;
import com.example.yrdev.ordering_system.User_Login_Register.LoginActivity;
import com.example.yrdev.ordering_system.Utils.GlobalFunction;
import com.example.yrdev.ordering_system.Utils.MIScanner;

import java.io.File;
import java.util.Locale;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by yrdev on 8/18/16.
 */
public class MenuActivity extends CommonActivity {
   @InjectView(R.id.option_menu)GridView  grid_View;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();

    }

    @Override
    public void initView() {
        super.initView();


        setBarTitle(getResources().getString(R.string.menu));
        showBackButton();

        initNFCParam();


        setContentView(R.layout.main_menu_layout);
        ButterKnife.inject(this);

        Integer[] images = {R.drawable.menu_report, R.drawable.menu_refund, R.drawable.menu_recharge
                , R.drawable.menu_save_info, R.drawable.menu_version, R.drawable.menu_setting, R.drawable.menu_cancel_sale,R.drawable.menu_logout};

        String[] title={getResources().getString(R.string.report),getResources().getString(R.string.refund)
                ,getResources().getString(R.string.recharge),getResources().getString(R.string.save_info),getResources().getString(R.string.version)
                ,getResources().getString(R.string.setting),getResources().getString(R.string.cancel_sale),"退出"};


        grid_View.setAdapter(new GridViewAdapter(this, images,title));
        grid_View.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

                switch (view.getId()) {

                    case R.drawable.menu_report:
                        gotoActivity(MenuReportActivity.class, 0);

                        break;
                    case R.drawable.menu_refund:
                        gotoActivity(MenuReturnGoodActivity.class, 0);
                        break;

                    case R.drawable.menu_save_info:
                        showDBSize();
                        break;

                    case R.drawable.menu_recharge:
                        showRechargeDialog();
                        break;
                    case R.drawable.menu_version:
                        showProductVersion();
                        break;

                    case R.drawable.menu_cancel_sale:

                       gotoActivity(MenuCancelPurchase.class,0 );

                        break;

                    case R.drawable.menu_setting:

                        if (Operator.getIntance().settingAuthority()){

                            gotoActivity(MenuSettingActivity.class, 0);

                        }else {

                            GlobalFunction.ToastAlert(MenuActivity.this,"非常抱歉，您没有设置权限");
                        }

                        break;

                    case R.drawable.menu_logout:

            logOut();

                        break;

                }
                activatePushingAnim();
            }
        });

    }

    PaymentDialog paymentDialog;

    public void showRechargeDialog() {

        reader.setOperate(1);
        reader.setAction(0);
        reader.setPurchaseMode(1);
        reader.setCallback(new MIScanner.Callback() {
            @Override
            public void getCardInfo(Double reduceAmount, Double remain, Double currentBalance) {

                GlobalFunction.ToastAlert(MenuActivity.this,"充值成功");
                paymentDialog.showOtherNominal(reduceAmount,remain,currentBalance);

            }

            @Override
            public void getMICardNo(String cardNo, String ticketID) {

            }

            @Override
            public void showFixPurchaseCost(Double cost) {

            }
        });

       paymentDialog  =new PaymentDialog(this,1,this);
        paymentDialog.show();
        paymentDialog.changeTitle("充值");
        paymentDialog.setCallBack(new PaymentDialog.callBack() {
            @Override
            public void getCurAmount(Integer amount) {
            reader.setReduceAmount(amount);
                processCardState=true;

            }

            @Override
            public void done() {

            }

            @Override
            public void standbyMode() {

            }
        });

        paymentDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialogInterface) {
                reader.reset();
                processCardState=true;
            }
        });


        processCardState=false;

    }

private   void logOut(){

        Intent intent=new Intent(this,LoginActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);


    }
    public void showDBSize(){

        File f = getDatabasePath(DingHanDB.DATABASE_NAME);
        double dbSize = f.length()/1000.;

        String content = "数据库大小 : " +String.format(Locale.getDefault(),"%.2f",dbSize);


        AlertDialog.Builder builder = new AlertDialog.Builder(this).setTitle("存储信息").setMessage(content+"Kb");
        builder.show();

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        restart();

    }

    public void showProductVersion() {

        String content = "型号 : " +Build.MODEL+"\n"+"版本号 : "+ Build.SERIAL;

        AlertDialog.Builder builder = new AlertDialog.Builder(this).setTitle("产品版本").setMessage(content);
        builder.show();

    }

    @Override
    protected void onResume() {

        super.onResume();

        try {

            if (processCardState)
            reader.processIntent(mNfcAdapter, getIntent());

        } catch (Exception e) {

        }

    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);

        try {
            if (processCardState)
            reader.processIntent(mNfcAdapter, intent);
        } catch (Exception e) {

        }


    }
}
